import { useRequest } from 'ahooks';
import { setStorage, getStorage } from '@/utils/storage';
import { fetchSettlementDateWholeDates } from '@/pages/BasicInfo/services';
import { isEmptyObj } from '@/utils/util';
import { useState } from 'react';

export default function Page() {
  const [settlementWeek, setSettlementWeek] = useState(getStorage('settlementWeek'));
  // 获取结算周信息
  const { runAsync: fetchAllAddress, loading: loading } = useRequest(async (isRefresh: boolean | undefined = false) => {
    const weekData = getStorage('settlementWeek');
    if (!isRefresh && !isEmptyObj(weekData)) {
      return weekData;
    }
    const res = await fetchSettlementDateWholeDates();
    setStorage('settlementWeek', res);
    setSettlementWeek(res);
    return res;
  }, {manual: true});

  // 刷新地址
  const refreshSettlementWeek = () => {
    fetchAllAddress(true)
  }


  // 获取地址数据
  const fetchSettlementWeek = () => {
    if (settlementWeek?.length) {
      return settlementWeek;
    }
    fetchAllAddress();
  }

 
  return {
    settlementWeek,
    fetchSettlementWeek,
    refreshSettlementWeek,
    loading,
  };
};  